package com.rtdance.server;

import java.util.Map;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.rtdance.client.GreetingService;

@SuppressWarnings("serial")
public class GreetingServiceImpl extends RemoteServiceServlet implements GreetingService {
	public Boolean greetServer(Map<String, String> valueMap){
		 Properties props = new Properties();
	        Session session = Session.getDefaultInstance(props, null);

	        String msgBody = "Contact number " + valueMap.get("contact") + "\n";
	        msgBody += "Name : " + valueMap.get("name") + "\n";
	        msgBody += "Email : " + valueMap.get("email") + "\n";
	        msgBody += "Message : \n" + valueMap.get("message"); 
	        

	        try {
	            Message msg = new MimeMessage(session);
	            msg.setFrom(new InternetAddress( "paras.dattani@gmail.com", "Business Enquiry"));
	            msg.addRecipient(Message.RecipientType.TO,
	                             new InternetAddress("master@rtdance.in", "RTDance support"));
	            msg.setSubject("Business Enquiry from Mr/Ms," + valueMap.get("name"));
	            msg.setText(msgBody);
	            Transport.send(msg);
	        } catch (Throwable e) {
	        	Logger.getAnonymousLogger().log(Level.SEVERE, "Problem in sending mails" , e);
	        	return false;
	        }
		
		return true;
	}
	
	boolean isEmpty(String value){
		return value==null || value.isEmpty() || value.trim().isEmpty();
	}

}
